﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using RBS.Core.Model;
using RBS.Web.API.Models;

namespace RBS.Web.API.Areas.User.Controllers
{
    /// <summary>
    /// 食客好友
    /// </summary>
    public class FriendsController : ApiController
    {
        private ApiContext db = new ApiContext(); 
        /// <summary>
        /// 获取好友列表
        /// </summary>
        /// <param name="id">当前用户ID</param>
        /// <returns>JsonData</returns>
        public JsonData GetU_Friends(int id)
        {
            var jsonData = new JsonData { Success = true }; 
            try
            {
                var item = db.U_Friends.Where(c => c.UserId == id).Select(c=>new
                    {
                         c.Id,
                         c.UserId,
                         c.FriendId,
                         c.FriendName,
                         c.Mobile
                    }); 
                jsonData.Data = item;
            }
            catch(Exception exception)
            {
                jsonData.Error = exception.Message.ToString();
                jsonData.Success = false;
            }  
            return jsonData;
        }  
      
        /// <summary>
        /// 添加好友
        /// </summary>
        /// <param name="valus"></param>
        /// <returns></returns>
        public JsonData AddFriends([FromBody] U_Friends valus)
        {
            var jsonData = new JsonData();
            /* if (model.FriendId == 0)
             {
                 var mobile = model.FriendName;
                 string content = string.Format("尊敬的朋友您的好友{0}邀请您使用呷饭网。点击下面地址下载呷饭网客户端:{1}", CurrentUser.U_Name, "http://www.xiafanwang.com/");
                 string msg = string.Empty;
                 bool result = new RBS.Core.BLL.SmsMessage().Send(mobile.Trim(), content, 2, out msg);
                 if (result)
                 {
                     ret.Msg = msg; 
                 }
                 else
                 {
                     ret.Msg = msg + content; 
                 }
                 var stcript = "$.dialog.alert('" + ret.Msg + "！');";
                 return Content(stcript, "application/x-javascript");  
                 return View();
             }*/

            var model = new RBS.Core.Model.U_Friends
                {
                    UserId = valus.UserId,
                    FriendId = valus.FriendId,
                    FriendName = valus.FriendName,
                    CreateTime = DateTime.Now,
                    Remark = valus.Remark
                }; 
            var bll = new RBS.Core.BLL.U_Friends();
            if (bll.ExistsFriendId(model.FriendId, model.UserId))
            {
                jsonData.Success = false;
                jsonData.Error = "当前用户已经是你好友";
                return jsonData;
            }
            if (bll.Add(model) > 0)
            {
                jsonData.Success = true;
                jsonData.Error = "添加成功";

            }
            return jsonData;
        }

      

        /// <summary>
        /// 删除好友
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns>JsonData</returns>
        public JsonData DeleteU_Friends(int id)
        {
            var jsonData = new JsonData {Success = true};
            var uFriends = db.U_Friends.Find(id);
            if (uFriends == null)
            {
                jsonData.Success = false;
                jsonData.Error = "未找到该好友";
                return jsonData;
            } 
            db.U_Friends.Remove(uFriends); 
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            { 
                jsonData.Success = false;
                jsonData.Error = Request.CreateErrorResponse(HttpStatusCode.NotFound, ex).ToString();
                return jsonData;
            }
            jsonData.Data=Request.CreateResponse(HttpStatusCode.OK, uFriends);
            return jsonData;
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}